#include <stdio.h>
#include <stdlib.h>

#include "securec.h"
#include "treeUtils.h"


struct TreeNode* construct(const int* nums, int left, int right) {
//    if (left > right) {
//        return NULL;
//    }
//    int best = left;
//    for (int i = left + 1; i <= right; ++i) {
//        if (nums[i] > nums[best]) {
//            best = i;
//        }
//    }
//    struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
//    node->val = nums[best];
//    node->left = construct(nums, left, best - 1);
//    node->right = construct(nums, best + 1, right);
//    return node;
    if (left > right) {
        return NULL;
    }
    int best = left;
    for (int i = left + 1; i < right; ++i) {
        if (nums[i] > nums[best]) {
            best = i;
        }
    }
    struct TreeNode *node = calloc(1, sizeof(struct TreeNode));
    node->val = nums[best];
    node->left = construct(nums, left, best - 1);
    node->right = construct(nums, best + 1, right);
    return node;
}

struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize){
    return construct(nums, 0, numsSize - 1);
}

int main() {
    char str[] = "[1,3,null,null,2]";
    printf("%s\n", "start strtok string.");
    int count = 0;
    char retStrArr[100][10] = {0};
    getNum(str, retStrArr, &count);
    int numArr[9] = {0};
    for (int i = 0; i < count; ++i) {
        printf("%s\n", retStrArr[i]);
        numArr[i] = atoi(retStrArr[i]);
    }
    printf("%s\n", "end strtok string.");

    struct TreeNode *root = malloc(sizeof(struct TreeNode));
    root->val = numArr[0];
    root->left = NULL;
    root->right = NULL;
    bfsBuildTree(root, numArr, count);

    int test[6] = {3,2,1,6,0,5};
    struct TreeNode* ret =  constructMaximumBinaryTree(test, 6);
}